package com.shujia.jinjie;

import com.shujia.utils.MySQLTool;
import com.shujia.utils.RedisTool;
import redis.clients.jedis.JedisCluster;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
import java.util.Set;

public class SelectGoodsFromIndex {
    public static void main(String[] args) throws Exception {
        //创建键盘录入对象
        Scanner sc = new Scanner(System.in);

        //从mysql中获取华为或者联想数据的id值存储到redis中
        Connection conn = MySQLTool.getConnection();

        // select * from jd_goods where id in (7,33,31);
//        PreparedStatement prep = conn.prepareStatement("select * from jd_goods where id=?");

        //创建与redis的连接对象
        JedisCluster jedisCluster = RedisTool.getConnection();

        System.out.println("请输入要查询的电脑品牌：");
        String pinPin = sc.next();
        Set<String> ids = jedisCluster.smembers("pinPai:" + pinPin);
        if(!ids.isEmpty()){
            StringBuilder sb = new StringBuilder();
            sb.append("(");
            int i = 1;
            for (String id : ids) {
                if (i == ids.size()) {
                    sb.append(id).append(")");
                    break;
                }
                sb.append(id).append(",");
                i++;
            }

            Statement statement = conn.createStatement();

            ResultSet resultSet = statement.executeQuery("select * from jd_goods where id in " + sb);


//        ResultSet resultSet = prep.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                String goods_name = resultSet.getString(2);
                String price = resultSet.getString(3);
                String comments = resultSet.getString(4);
                String shop = resultSet.getString(5);
                String icos = resultSet.getString(6);

                System.out.println(id + "-" + goods_name + "-" + price + "-" + comments + "-" + shop + "-" + icos);
            }
        }



//        prep.close();
        jedisCluster.close();
        conn.close();

    }
}
